<?php

class searchlocation{
public function index(){
$city_id = intval($GLOBALS['request']['city_id']);
$cate_id= intval($GLOBALS['request']['cate_id']);
$cate_type_id = intval($GLOBALS['request']['cate_type_id']);
$ordertype=$GLOBALS['request']['order_by_type'];
$search_type= intval($GLOBALS['request']['search_type']);
$page = intval($GLOBALS['request']['page']);
$page=$page==0?1:$page;
$page_size = PAGE_SIZE;
$limit = (($page-1)*$page_size).','.$page_size;
$distance = intval($GLOBALS['request']['distance']);
$lat = trim($GLOBALS['request']['log']);
$log =trim($GLOBALS['request']['lat']);
$key_word=$GLOBALS['request']['key_word'];
$key_word_unicode = str_to_unicode_string($key_word);
$order_type=get_order_type($ordertype);
if($search_type==0 or empty($search_type)){
if(empty($cate_type_id ) or $cate_type_id=='null'){
$sql_count = 'SELECT COUNT(id)  FROM '.DB_PREFIX."supplier_location  WHERE match locate_match against('".$key_word_unicode."'  IN BOOLEAN MODE)   AND deal_type=0 and  city_id='{$city_id}'    ";
$count=$GLOBALS['db']->getOne($sql_count);
$sql= 'SELECT id,name,address,preview,avg_point,xpoint,ypoint,avg_price as price_type  FROM '.DB_PREFIX."supplier_location    WHERE match locate_match against('".$key_word_unicode."'  IN BOOLEAN MODE)   AND deal_type=0 and   city_id='{$city_id}'   order by {$order_type} desc limit ".$limit;
}else
{
$sql_count = 'SELECT COUNT(sl.id)  FROM '.DB_PREFIX.'supplier_location as sl left join '.DB_PREFIX."deal_cate_type_location_link as ct on sl.id=ct.location_id  WHERE match sl.locate_match against('".$key_word_unicode."' IN BOOLEAN MODE)  AND deal_type=0 and ct.deal_cate_type_id='{$cate_type_id}' and sl.city_id='{$city_id}'   ";
$count=$GLOBALS['db']->getOne($sql_count);
$sql= 'SELECT sl.id,sl.name,sl.address,sl.preview,sl.avg_point,sl.xpoint,sl.ypoint,sl.avg_price as price_type  FROM '.DB_PREFIX.'supplier_location as sl  left join  '.DB_PREFIX."deal_cate_type_location_link as dctl on sl.id=dctl.location_id  WHERE match sl.locate_match against('".$key_word_unicode."' IN BOOLEAN MODE)   AND sl.deal_type=0 and  dctl.deal_cate_type_id='{$cate_type_id}' and sl.city_id='{$city_id}'  order by sl.{$order_type} desc  limit ".$limit;
}
$list= $GLOBALS['db']->getAll($sql);
$root['menu']=  $GLOBALS['db']->getAll('SELECT id as trad_id,city_id,name FROM '.DB_PREFIX."area WHERE pid=0 and city_id='{$city_id}' ");
foreach($list as $k=>$v)
{
$list[$k]['point']=score2Half($v['avg_point']);
$list[$k]['distance']=GetDistance($lat,$log,$v['xpoint'],$v['ypoint']);
$list[$k]['image']=get_abs_img_root(get_spec_image($v['preview'],640,240,1));
$dish=$GLOBALS['db']->getAll('SELECT ct.name   FROM '.DB_PREFIX.'deal_cate_type_location_link as ctl left join '.DB_PREFIX."deal_cate_type as ct on ctl.deal_cate_type_id=ct.id  WHERE  ctl.location_id='{$v['id']}' ");
$dish_array=array();
foreach($dish as $dk=>$dv)
{
array_push($dish_array,$dv['name']);
}
$list[$k]['specialty_dish']= implode('  ',$dish_array);
$list[$k]['avg_price_format'] = '￥'.$GLOBALS['lang']['AVG_PRICE_'.$v['price_type']];
if(empty($list[$k]['specialty_dish']))
{
unset($list[$k]['specialty_dish']);
}
unset($list[$k]['preview'],$list[$k]['xpoint'],$list[$k]['avg_point'],$list[$k]['ypoint'],$list[$k]['price_type']);
}
}else{
$around=getAround($lat,$log,$distance);
$where=" sl.city_id='{$city_id}' AND deal_type=0 AND sl.xpoint > '{$around['minLat']}' AND sl.xpoint < '{$around['maxLat']}' AND sl.ypoint > '{$around['minLng']}' AND sl.ypoint < '{$around['maxLng']}' and slm.name like '%".$key_word."%' ";
if(empty($cate_type_id))
{
$from=' '.DB_PREFIX.'supplier_location_menu as slm left join '.DB_PREFIX.'supplier_location as sl on slm.supplier_location_id=sl.id ';
$sql_count = "SELECT COUNT(slm.id)   FROM {$from}   where {$where} ";
$sql="SELECT slm.id as menu_id,slm.supplier_location_id as supplier_id,slm.name as menu_name,slm.price,slm.preview,sl.name as supplier_name,sl.xpoint,sl.ypoint,slm.collect_nums,sl.menu_count  FROM {$from}  where {$where} order by sl.{$order_type} limit ".$limit;
}else{
$sql_count = 'SELECT COUNT(dctm.menu_id)   FROM ('.DB_PREFIX.'deal_cate_type_menu_link as dctm left join '.DB_PREFIX.'supplier_location_menu as slm on slm.id=dctm.menu_id) left join '.DB_PREFIX."supplier_location as sl on sl.id=slm.supplier_location_id  where {$where} and deal_cate_type_id='{$cate_type_id}'  ";
$sql='SELECT dctm.menu_id,slm.supplier_location_id as supplier_id,slm.name as menu_name,slm.price,slm.preview,sl.name as supplier_name,sl.xpoint,sl.ypoint,slm.collect_nums,sl.menu_count  FROM ('.DB_PREFIX.'deal_cate_type_menu_link as dctm left join '.DB_PREFIX.'supplier_location_menu as slm on slm.id=dctm.menu_id) left join '.DB_PREFIX."supplier_location as sl on sl.id=slm.supplier_location_id  where {$where} and deal_cate_type_id='{$cate_type_id}' order by sl.{$order_type} limit ".$limit;
}
$count=$GLOBALS['db']->getOne($sql_count);
$list=  $GLOBALS['db']->getAll($sql);
foreach($list as $key=>$value)
{
$list[$key]['distance']=GetDistance($lat,$log,$value['xpoint'],$value['ypoint']);
$list[$key]['price']='￥'.round($value['price'],1);
if(empty($value['preview'])){
$list[$key]['image']='';
}else{
$list[$key]['image']=get_abs_img_root(get_spec_image($value['preview'],400,240,1));
}
unset($list[$key]['preview'],$list[$key]['xpoint'],$list[$key]['ypoint']);
}
}
$page_total = ceil($count/$page_size);
$root['item']=$list;
$root['page'] = array('page'=>$page,'page_total'=>$page_total);
output($root);
}

?>